﻿using System.Drawing;
using System.Text.RegularExpressions;
using com.fukefu.source;
using com.fukefu.tools;
using com.fukefu.vcode;

namespace com.fukefu.info.b2b.byhand
{
    public class ShengYiBao : Information
    {
        string v_id = "", v_digest = "", referer, login_name = "";
        VCODE code = null;
        //登陆
        public override bool Login(string name, string pass, string vcode)
        {
            ParametersData pd = new ParametersData("gb2312");
            pd.Add("f", "login");
            pd.Add("returl", "");
            pd.Add("username", name);
            pd.Add("password", pass);
            pd.Add("f1", "登 录");

            string html = source.Post("http://china.toocle.com/member/index.cgi", pd.Read(), "http://china.toocle.com/member/");
            if (html.IndexOf("/member/Action0.cgi?" + name) != -1)
            {
                html = source.Get("http://china.toocle.com/member/Action0.cgi");
                referer = "http://china.toocle.com/member/" + Regex.Match(html, "<a href=\"(Trade\\.cgi\\?t=trade&f=add&netsun&0\\.[0-9]+)[^>]+>发布商机</a>").Groups[1].Value;
                this.login_name = name;
                code = new VCODE(site);
                return true;
            }
            return false;
        }
        //刷新验证码
        public override Image GetFlushCodeImage()
        {
            string html = source.Get("http://china.toocle.com/bin/gen_check_t.cgi?" + MyFunction.GetTimestampNow());
            if (!html.Equals(""))
            {
                v_id = html.Split('|')[0];
                v_digest = html.Split('|')[1];
            }
            return source.GetImage("http://china.toocle.com/trade/pic_member.cgi?color=000000&id=" + v_id + "&digest=" + v_digest, referer);
        }
        //验证码识别系统
        public string GetFlushCodeImage2()
        {
            string html = source.Get("http://china.toocle.com/bin/gen_check_t.cgi?" + MyFunction.GetTimestampNow());
            if (!html.Equals(""))
            {
                v_id = html.Split('|')[0];
                v_digest = html.Split('|')[1];
            }
            return code.Get(source.GetStream("http://china.toocle.com/trade/pic_member.cgi?color=000000&id=" + v_id + "&digest=" + v_digest, referer));
        }
        //发布文章
        public override InformationResult PostArticle(string title, string content, string code)
        {
            InformationResult info = new InformationResult();
            content = Regex.Replace(content, "<p>\\s*</p>", "", RegexOptions.IgnoreCase);
            SourceFormData sfd = new SourceFormData("gb2312");
            code = GetFlushCodeImage2();
            //头部
            sfd.Write("id", "");
            sfd.Write("qid", "");
            sfd.Write("column_cate", "");
            sfd.Write("cate", "Chinese");
            sfd.Write("website_name", "");
            sfd.Write("tag", "");
            sfd.Write("f", "adding882");
            sfd.Write("t", "trade");
            sfd.Write("poster", login_name);
            sfd.Write("type", "12");
            sfd.Write("product", title);
            sfd.Write("title", title);
            sfd.Write("c1", site.TypeId[0]);
            sfd.Write("c2", site.TypeId[1]);
            sfd.Write("subcategory", site.TypeId[2]);
            sfd.Write("expiryDate", "0");
            sfd.Write("input_12_1", pif.Get("库存数量") + "台");
            sfd.Write("input_12_2", pif.Get("产品描述"));
            sfd.Write("input_12_3", pif.Get("产品价格") + "元/台");
            sfd.Write("input_12_4", ktag.Get(3, ","));
            sfd.Write("intro", content);
            sfd.Write("in_Q", "1");
            sfd.Write("Q", "18652+");
            sfd.Write("Q_tmp", "18652");
            //发送文件
            sfd.WriteFile("pic1", RandomPicture.Get());
            //尾部
            sfd.Write("company", pif.Get("公司"));
            sfd.Write("contact", pif.Get("联系人"));
            sfd.Write("address", pif.Get("地址"));
            sfd.Write("zip", "");
            sfd.Write("tel", pif.Get("电话"));
            sfd.Write("mobile", pif.Get("手机号码"));
            sfd.Write("fax", "");
            sfd.Write("email", pif.Get("EMAIL"));
            sfd.Write("company_http", pif.Get("网址"));
            sfd.Write("v_id", v_id);
            sfd.Write("v_digest", v_digest);
            sfd.Write("v_secret", code);
            sfd.Write("submit", "确认");
            //结束
            string html = source.Post("http://china.toocle.com/member/Trade.cgi?t=trade&f=add&netsun", sfd.GetContentType(), sfd.Read(), "http://china.toocle.com/member/Trade.cgi?t=trade&f=add&netsun");
            if (html.IndexOf("商业信息已成功发布") != -1)
            {
                info.Code = 1;
                info.Content = Regex.Match(html, "<a href=\"(http://china.toocle.com/trade/detail--[0-9]+.html)\" class=\"t_01s\" target=_blank>请点这里查看</a>").Groups[1].Value;
            }
            else if (html.IndexOf("单用户最多可以发布500条商机") != -1)
            {
                info.Code = 2;
            }
            else if (html.EndsWith("<div class=\"main_right\">"))
            {
                info.Content = "网站不稳定,发送失败,推荐更换IP重新登陆或稍等一会!";
            }
            else if (Regex.IsMatch(html, "<div class=\"main_right\">(.+?),<a href=javascript:history"))
            {
                info.Content = Regex.Replace(Regex.Match(html, "<div class=\"main_right\">(.+?),<a href=javascript:history").Groups[1].Value, "<.+?>", "");
            }
            else if (Regex.IsMatch(html, "<p align=center>([^>]+)<a href=javascript:history.back(-1)>返回</a>"))
            {
                info.Content = Regex.Match(html, "<p align=center>([^>]+)<a href=javascript:history\\.back\\(-1\\)>返回</a>").Groups[1].Value;
            }
            return info;
        }
    }
}
